package cn.percent.executor;

import cn.percent.config.BoundSql;
import cn.percent.config.Configuration;
import cn.percent.config.MappedStatement;
import cn.percent.utils.GenericTokenParser;
import cn.percent.utils.ParameterMapping;
import cn.percent.utils.ParameterMappingTokenHandler;


import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class SimpleExecutor extends BaseExecutor{
    public <E> List<E> query(Configuration configuration, MappedStatement mappedStatement,Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, InvocationTargetException, IntrospectionException {
        Connection connection = configuration.getDataSource().getConnection(configuration.getEnvironmentId());

        String sql = mappedStatement.getSql();
        System.out.println(sql);
        BoundSql boundSql = getBoundSql(sql);
        System.out.println(boundSql.getSqlText());

        PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText());

        String paramterType = mappedStatement.getParamterType();

        System.out.println(paramterType);

        Class<?> parameterTypeClass = getClassType(paramterType);

        List<ParameterMapping> parameterMappingList = boundSql.getParameterMappingList();



        for (int i = 0; i < parameterMappingList.size(); i++) {
            ParameterMapping parameterMapping = parameterMappingList.get(i);
            String content = parameterMapping.getContent();

            Field declaredField = parameterTypeClass.getDeclaredField(content);
            declaredField.setAccessible(true);
            Object o = declaredField.get(params[0]);
            System.out.println(o);
            preparedStatement.setObject(i+1,o);
        }

        ResultSet resultSet = preparedStatement.executeQuery();
        String resultType = mappedStatement.getResultType();
        Class<?> resultClassType = getClassType(resultType);
        ArrayList<Object> objects = new ArrayList<>();

        while (resultSet.next()) {
            Object o = resultClassType.newInstance();
            ResultSetMetaData metaData = resultSet.getMetaData();
            for (int i = 1; i<=metaData.getColumnCount(); i++){
                String columnName = metaData.getColumnName(i);
                Object value = resultSet.getObject(columnName);
                PropertyDescriptor propertyDescriptor = new PropertyDescriptor(columnName, resultClassType);
                Method writeMethod = propertyDescriptor.getWriteMethod();
                writeMethod.invoke(o,value);
            }
            objects.add(o);
        }


        return (List<E>) objects;
    }

    public void execute(Configuration configuration, MappedStatement mappedStatement,Object... params) throws SQLException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
        Connection connection = configuration.getDataSource().getConnection(configuration.getEnvironmentId());
        connection.setAutoCommit(false);
        String sql = mappedStatement.getSql();
        BoundSql boundSql = getBoundSql(sql);
        PreparedStatement preparedStatement = connection.prepareStatement(boundSql.getSqlText());
        String paramterType = mappedStatement.getParamterType();
        Class<?> parameterTypeClass = null;
        System.out.println(paramterType);
//        if (paramterType.equals(Integer.TYPE.toString())){
//            parameterTypeClass = Integer.class;
//        } else {
//            parameterTypeClass = getClassType(paramterType);
//        }
        parameterTypeClass = getClassType(paramterType);
        System.out.println(boundSql.getSqlText());
        List<ParameterMapping> parameterMappingList = boundSql.getParameterMappingList();
        System.out.println(params);
        for (int i = 0;i < parameterMappingList.size(); i++) {
            ParameterMapping parameterMapping = parameterMappingList.get(i);
            String content = parameterMapping.getContent();
            System.out.println(content);
            Field declaredField = parameterTypeClass.getDeclaredField(content);
            declaredField.setAccessible(true);
            Object o = declaredField.get(params[0]);
            System.out.println(o);
            preparedStatement.setObject(i+1,o);
        }
        preparedStatement.execute();
        connection.commit();
    }


}
